//
//  ZyView.m
//  ShortRent
//
//  Created by ht on 16/6/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYChoiceView.h"

@implementation ZYChoiceView


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setTitleString:(NSString *)titleString
{
    _titleString = titleString;
    _tableViewHeadLbl.text =_titleString;
}
-(void)setItemString:(NSString *)itemString
{
    _itemString  = itemString;
    [_closetBtn setTitle:_itemString forState:UIControlStateNormal];
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        
        _current = -1;
        
        self.backgroundColor = [UIColor clearColor];
        
        CGFloat sbw = frame.size.width/3 *2;
        CGFloat sbh = frame.size.height/4 *2;
        
        UIButton *bkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        bkBtn.backgroundColor = [UIColor blackColor];
        bkBtn.alpha = 0.3f;
        [self addSubview:bkBtn];
        bkBtn.frame = CGRectMake(0, 0, frame.size.width, [UIScreen mainScreen].bounds.size.height);
        [bkBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        
        _backView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, sbw, sbh)];
        [self addSubview:_backView];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, sbw, sbh-44) style:UITableViewStylePlain];
        _tableView.delegate =self;
        _tableView.dataSource =self;
        [_backView addSubview:_tableView];
        _tableView.tableFooterView = [[UIView alloc] init];
        
        //        头部 显示的UILabel
        _tableViewHeadLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sbw,44)];
        _tableViewHeadLbl.textAlignment = NSTextAlignmentCenter;
        _tableViewHeadLbl.textColor = [UIColor grayColor];
        _tableViewHeadLbl.font = [UIFont boldSystemFontOfSize:22.0f];
        _tableViewHeadLbl.backgroundColor = [UIColor colorWithRed:214.0f/255.0f green:214.0f/255.0f  blue:214.0f/255.0f  alpha:1.0f];
        [_backView addSubview:_tableViewHeadLbl];
        
        //        头部 显示的UIButton
        _closetBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [_closetBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        _closetBtn.frame = CGRectMake(sbw - 44, 0, 44, 44);
        [_closetBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_closetBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _closetBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        _closetBtn.backgroundColor = [UIColor colorWithRed:214.0f/255.0f green:214.0f/255.0f  blue:214.0f/255.0f  alpha:1.0f];
        [_backView addSubview:_closetBtn];
    }
    return self;
}



+(ZYChoiceView *)shareChoiceView
{
    CGFloat viewStarY= [[UINavigationBar appearance] isTranslucent]?64:0.0;
    ZYChoiceView *choiceView = [[ZYChoiceView  alloc] initWithFrame:CGRectMake(0, viewStarY, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    return choiceView;
}

-(void)zyCellTitle:(NSString *(^)(NSIndexPath *))cellCompltion andCellCount:(NSInteger (^)(void))cellCoutCompltion andAction:(void (^)(NSIndexPath *))didViewCompletion
{
    self.cellTitle = [cellCompltion copy];
    self.cellCount  = [cellCoutCompltion copy];
    self.didViewCompletion = [didViewCompletion copy];
    [_tableView reloadData];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.cellTitle(indexPath);
    cell.selectionStyle =  UITableViewCellSelectionStyleBlue;
    
    if(_current == indexPath.row){
        cell.accessoryType =  UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType =  UITableViewCellAccessoryNone;
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.cellCount == nil) {
        return 0;
    }
    return self.cellCount();
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    self.didViewCompletion(indexPath);
    
    _current = indexPath.row;
    [_tableView reloadData];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    //    [tableView setSeparatorColor:UIColorFromRGBA(0xe1e1e1, 1)];
}

-(void)show
{
    _backView.center = CGPointMake(self.bounds.size.width/2,-self.bounds.size.height/2);
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        _backView.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height/2);
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    } completion:^(BOOL finished) {
        
    }];
}

-(void)dismiss
{
    _backView.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height/2);

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        
        _backView.center = CGPointMake(self.bounds.size.width/2,self.bounds.size.height *2);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];

    
    
}

@end
